#include<stdio.h>
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <linux/in.h>  //sockaddr_in
#include <stdlib.h>
#include <sys/select.h>
#include <string.h>

int main(int argc, const char *argv[])
{
	int socketfd,set,accfd;

	socketfd = socket(AF_INET,SOCK_STREAM,0); //创建套接子
	if(socketfd == 0)
	{
		perror("err socket");
		return 1; 
	}
	
	struct sockaddr_in saddr;
	saddr.sin_family = AF_INET;
	saddr.sin_port = htons(8888);
	saddr.sin_addr.s_addr = inet_addr("127.0.0.1");  //绑定

	set = bind(socketfd,(struct sockaddr *)&saddr,sizeof(saddr));
	if(set < 0)
	{
		perror("bind err");
		return 1;
	}
	if( listen(socketfd,11)<0 )		//监听
	{
		perror("listen err");
		return -1;
	}
	ssize_t rec;
	char buf[32] = "0";
	while(1)
	{
		accfd = accept(socketfd,NULL,NULL);  	//连接
		rec = recv(accfd,buf,10,0);			//接收
		if(rec < 0)
		{
			perror("recv err");
			return -1;
		}
		send(accfd,buf,strlen(buf),0);		//发送
	}
	
	close(socketfd);
	close(accfd);
	return 0;
}
